package com.kongxx.wicketstudy.tutorial;

import org.apache.wicket.Component;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.Session;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authorization.IAuthorizationStrategy;
import org.apache.wicket.protocol.http.WebApplication;

import com.kongxx.wicketstudy.tutorial.login.Login;
import com.kongxx.wicketstudy.tutorial.login.LoginSession;

public class WicketApplication extends WebApplication {

	public WicketApplication() {
	}

	public Class getHomePage() {
		return Home.class;
	}

	public Session newSession(Request request, Response response) {
		return new LoginSession(WicketApplication.this, request);
	}
	
	protected void init() {
		getSecuritySettings().setAuthorizationStrategy(
			new IAuthorizationStrategy() {
				public boolean isActionAuthorized(Component component,
						Action action) {
					return true;
				}

				public boolean isInstantiationAuthorized(
						Class componentClass) {
					if (AuthenticatedWebPage.class.isAssignableFrom(componentClass)) {
						// Is user signed in?
						if (((LoginSession) Session.get()).isSignedIn()) {
							// okay to proceed
							return true;
						}

						// Force sign in
						throw new RestartResponseAtInterceptPageException(
								Login.class);
					}
					return true;
				}
			});
	}
}
